<?php
/**
 * Session controller, /session
 */
class SessionController extends BRail_Application_Controller_Abstract
{
    /**
     * Switch language
     * @return void
     */
    public function languageAction()
    {
        $locale = $this->_request->getParam('language', false);

        if ($locale === false) {
            return $this->_forward('index', 'index');
        }

        if (in_array($locale, Zend_Registry::get('Zend_Application')->getLocales())) {
            // We can't use set cookie like that ** setcookie('locale', $locale, null, '/');
            // because that function is not available in the command line based Unit Tests
            // we have to manipulate directly the HTTP response object
            $this->getResponse()->setHeader('Set-Cookie', Application::code . '_locale=' . $locale . ';path=/');
            Zend_Registry::get('Zend_Locale')->setDefault($locale);
        }

        return $this->_redirect(isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '/');
    }

    /**
     * Login action through BRail_LdapZendAuthTiein_Adapter
     *
     * If login is successful,
     * <ul>
     * <li>Set identity, an User instance, using Zend_Auth</li>
     * <li>Generate a authentification token, used for CSRF protection</li>
     * <li>redirect the user to /</li>
     * </ul>
     *
     * @return void
     * @see BRail_LdapZendAuthTiein_Adapter
     * @see Zend_Auth
     */
    public function newAction()
    {
        $this->_helper->layout->setLayout('login');

        $this->view->auth = Zend_Auth::getInstance();

        if ($this->getRequest()->isPost()) {
            $username = trim($this->getRequest()->getParam('username'));
            $password = trim($this->getRequest()->getParam('password'));
            $this->view->username = $username;

            // Check if posted values are valid otherwise we won't even try to auth
            if (empty($username)
                or empty($password)) {
                $this->view->error = 1;
                return;
            }

            // Check if user exist in database
            $adminTable = new AdminTable();
            $user = $adminTable->authenticateUser($username, $password);

            if (0 === count($user)) {
                // User does not exist in database
                Application::getLogger()->log($username . ' wanted to log into application but has no access',
                                              Zend_Log::NOTICE);
                $this->view->error = 1;
                return;
            }

            Application::getLogger()->log($username . ' successfully logged into application',
                                          Zend_Log::INFO);

            $session = new Zend_Session_Namespace('session');
            $session->authenticatedUser = $user->current()->login;

            return $this->_redirect('/');
        }
    }

    /**
     * Delete session and redirect users to homepage
     * @return void
     */
    public function destroyAction()
    {
        Zend_Auth::getInstance()->clearIdentity();
        Zend_Session::destroy();
        $this->_redirect('/');
    }
}